/*
 * @Description: 地图模式切换之后的鼠标操作提示
 * @Date: 2022-03-14 09:57:08
 * @Author: yeming
 */
import Overlay from 'ol/Overlay'
import { elementStr2Node } from './svg.js'

class MouseTips {
  constructor(ol2d, mouseTipsMap) {
    this.map2d = ol2d.map2d
    this.mouseTipsMap = mouseTipsMap
    this.sessionId = new Date().getTime()
  }

  /**
   * @desc: 添加提示
   * @param {Number} mapMode 地图模式
   * @param {Array} coordinate 屏幕坐标
   * @return {*}
   * @use: 使用示例
   */
  addMouseTips (mapMode, coordinate, content) {
    let element = '<div class="mouse-tips show">' + content + '</div>'
    const mouseTips = new Overlay({
      // rend3d: true, // 是否渲染3d 默认值为false
      id: this.sessionId,
      sessionId: this.sessionId,
      element: elementStr2Node(element),
      mapMode: mapMode,
      offset: [20, -30] // 图标中心点位置
    })
    mouseTips.setPosition(coordinate)
    this.map2d.addOverlay(mouseTips)

    this.mouseTipsMap.set(this.sessionId, { 'item2d': mouseTips })
  }

  changeMouseTipsPosition (coordinate, content) {
    let currentTips = this.mouseTipsMap.get(this.sessionId)
    if (!currentTips) return false

    let element = '<div class="mouse-tips show">' + content + '</div>'
    let currentOverLay = currentTips.item2d
    currentOverLay.setElement(elementStr2Node(element))
    currentOverLay.setPosition(coordinate)
  }

  delMouseTips () {
    let currentTips = this.mouseTipsMap.get(this.sessionId)
    if (!currentTips) return false

    let currentOverLay = currentTips.item2d
    this.map2d.removeOverlay(currentOverLay)
    this.mouseTipsMap.delete(this.sessionId)
  }
}

export default MouseTips

